package com.example.a15787.weather_zyy_1235;


import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Collections;
import java.util.List;

public class FavoritesAdapter extends RecyclerView.Adapter<FavoritesAdapter.ViewHolder> {
    private List<String> favorites;
    private boolean isEditMode = false;
    private CitySearchActivity activity;
    private OnStartDragListener dragStartListener;

    public interface OnStartDragListener {
        void onStartDrag(RecyclerView.ViewHolder viewHolder);
    }

    public FavoritesAdapter(List<String> favorites, CitySearchActivity activity, OnStartDragListener dragStartListener) {
        this.favorites = favorites;
        this.activity = activity;
        this.dragStartListener = dragStartListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_searched_city, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final String city = favorites.get(position);
        holder.tvCity.setText(city);

        // 编辑模式显示删除按钮和拖拽手柄
        holder.ivDelete.setVisibility(isEditMode ? View.VISIBLE : View.GONE);
        holder.ivDragHandle.setVisibility(isEditMode ? View.VISIBLE : View.GONE);

        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeItem(holder.getAdapterPosition());
            }
        });

        // 设置拖拽手柄的触摸监听
        holder.ivDragHandle.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, android.view.MotionEvent event) {
                if (isEditMode && dragStartListener != null) {
                    dragStartListener.onStartDrag(holder);
                    return true;
                }
                return false;
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEditMode) {
                    showPopupMenu(holder.itemView, holder.getAdapterPosition());
                } else {
                    activity.selectFavoriteCity(city);
                }
            }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showPopupMenu(v, holder.getAdapterPosition());
                return true;
            }
        });
    }

    private void showPopupMenu(View view, final int position) {
        PopupMenu popup = new PopupMenu(activity, view);
        popup.getMenuInflater().inflate(R.menu.city_item_menu, popup.getMenu());

        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_delete:
                        removeItem(position);
                        return true;
                    case R.id.action_move_top:
                        moveToTop(position);
                        return true;
                    default:
                        return false;
                }
            }
        });
        popup.show();
    }

    public void moveItem(int fromPosition, int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(favorites, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(favorites, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
        activity.saveFavorites();
    }

    public void moveToTop(int position) {
        if (position > 0) {
            String city = favorites.remove(position);
            favorites.add(0, city);
            notifyItemMoved(position, 0);
            activity.saveFavorites();
        }
    }

    public void removeItem(int position) {
        favorites.remove(position);
        notifyItemRemoved(position);
        activity.saveFavorites();
    }

    public void addItem(String city) {
        if (!favorites.contains(city)) {
            favorites.add(0, city);
            notifyItemInserted(0);
            activity.saveFavorites();
        } else {
            Toast.makeText(activity, "城市已在收藏列表中", Toast.LENGTH_SHORT).show();
        }
    }

    public void setEditMode(boolean editMode) {
        isEditMode = editMode;
        notifyDataSetChanged();
    }

    public boolean isEditMode() {
        return isEditMode;
    }

    @Override
    public int getItemCount() {
        return favorites.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvCity;
        ImageView ivDelete;
        ImageView ivDragHandle;

        public ViewHolder(View itemView) {
            super(itemView);
            tvCity = itemView.findViewById(R.id.tvSearchedCity);
            ivDelete = itemView.findViewById(R.id.ivDelete);
            ivDragHandle = itemView.findViewById(R.id.ivDragHandle);
        }
    }
}